#!/bin/bash
cat > /etc/init.d/overlay << EOF
#!/sbin/runscript

depend() {
	need checkroot
	before hostname
}

start() {
	ebegin "Overlaying files"
	/bin/tar -zx --no-same-owner -C / -f /mnt/cdrom/overlay.tgz
	eend \$? "Failed to overlay files"
	if [ -e /etc/overlay.conf ]; then
		source /etc/overlay.conf
		ebegin "Adding services"
		DONTDEPSCAN=1
		if [ "\${USE_SSH}" == "yes" ]; then
			ln -snf "/etc/init.d/dropbear" "/etc/runlevels/default/dropbear"
			DONTDEPSCAN=0
		fi
		if [ "\${USE_FW}" == "yes" ]; then
			ln -snf "/etc/init.d/iptables" "/etc/runlevels/boot/iptables"
			if [ "\${FW_TYPE}" == "firehol" ]; then
			  ln -snf "/etc/init.d/firehol" "/etc/runlevels/default/firehol"
			else
			  ln -snf "/etc/init.d/shorewall" "/etc/runlevels/default/shorewall"
			fi
			DONTDEPSCAN=0
		fi
		# Old syntax compatibility
		if [ "\${USE_CBQ}" == "yes" ]; then
		  USE_TC=yes
		  TC_TYPE=cbq
		fi
		if [ "\${USE_TC}" == "yes" ]; then
			if [ "\${TC_TYPE}" == "htb" ]; then
			  ln -snf "/etc/init.d/htbinit" "/etc/runlevels/default/htbinit"
			else
			  ln -snf "/etc/init.d/cbqinit" "/etc/runlevels/default/cbqinit"
			fi
			DONTDEPSCAN=0
		fi
		if [ "\${USE_VPN}" == "yes" ]; then
			ln -snf "/etc/init.d/openvpn" "/etc/runlevels/default/openvpn"
			DONTDEPSCAN=0
		fi
		if [ "\${USE_DNSDHCP}" == "yes" ]; then
			ln -snf "/etc/init.d/dnsmasq" "/etc/runlevels/default/dnsmasq"
			DONTDEPSCAN=0
		fi
		if [ -n "\${NTPSERVER}" ]; then
			ln -snf "/etc/init.d/ntpd" "/etc/runlevels/default/ntpd"
			DONTDEPSCAN=0
		fi
		if [ "\${USE_HTTP}" == "yes" ]; then
			ln -snf "/etc/init.d/boa" "/etc/runlevels/default/boa"
			DONTDEPSCAN=0
		fi
		if [ "\${USE_PPPOE}" == "yes" ]; then
			ln -snf "/etc/init.d/rp-pppoe" "/etc/runlevels/default/rp-pppoe"
			DONTDEPSCAN=0
		fi
		eend \${DONTDEPSCAN} "No service to add"

		if [ -n "\${NBCARDS}" ]; then
                  if [ \${NBCARDS} -ge 2 ]; then
		  	ebegin "Adding network cards"
			ln -snf "/etc/init.d/net.eth1" "/etc/runlevels/default/net.eth1"
			DONTDEPSCAN=0
			if [ \${NBCARDS} -ge 3 ]; then
				ln -snf "/etc/init.d/net.eth2" "/etc/runlevels/default/net.eth2"
			fi
		  	if [ \${NBCARDS} -ge 4 ]; then
				ln -snf "/etc/init.d/net.eth3" "/etc/runlevels/default/net.eth3"
		  	fi
			eend 0
		  fi
		fi

		if [ "\${FW_TYPE}" == "firehol" ]; then
			rm /etc/init.d/shorewall
		else
			rm /etc/init.d/firehol
		fi
		
		if [ \${DONTDEPSCAN} -eq 0 ]; then
			/sbin/depscan.sh
			eend 0
		fi
		
		if [ -n "\${NTPSERVER}" ]; then
			ebegin "Setting NTP server location"
			echo "server \${NTPSERVER}" >> /etc/ntpd.conf
			sed -i "s/^NTPD_OPTS=.*$/NTPD_OPTS=\"-s\"/" /etc/conf.d/ntpd
			eend \$? "Failed to set NTP server"
		fi
		if [ "\${EMPTY_ROOTPASS}" == "yes" ]; then
			ebegin "Setting an empty root password"
			sed -i "s/^root:[^:]*:/root::/" /etc/shadow
			eend \$? "Failed to modify the shadow file"
		fi
		if [ -n "\${TIMEZONE}" ]; then
			ebegin "Setting timezone to \$TIMEZONE"
			echo \${TIMEZONE} > /etc/TZ
			eend \$? "Failed to set timezone"
		fi
		if [ -n "\${KEYMAP}" ]; then
			ebegin "Setting keyboard map to \$KEYMAP"
			sed -i "s/^KEYMAP=\"us\"/KEYMAP=\"\$KEYMAP\"/" /etc/conf.d/keymaps
			eend \$? "Failed to set keymap"
		fi
		if [ "\${IP_RELAY}" == "yes" ]; then
			ebegin "Allowing IP routing"
			sed -i "s/#net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/" /etc/sysctl.conf
			eend \$? "Failed to modify sysctl.conf"
		fi
	fi
	return 0
}
EOF
chmod a+x /etc/init.d/overlay

# Hack to avoid autoconfig messing up the net config
sed -i "s/^DHCP=\"yes\"/DHCP=\"noact\"/" /etc/init.d/autoconfig

# We are a liveCD
echo "export CDBOOT=yes" >> /sbin/functions.sh

# Default firewall setup
cat > /var/lib/iptables/rules-save <<EOF
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [308:28160]
[308:28160] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
[0:0] -A INPUT -i lo -j ACCEPT
[0:0] -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT
EOF
sed -i "s/^SAVE_ON_STOP=.*$/SAVE_ON_STOP=\"no\"/" /etc/conf.d/iptables

# Undo livecdfs-update damage
rm /etc/hostname
rc-update add keymaps boot
sed -i "/^.*bashlogin.*$/ d" /etc/inittab
sed -i "s/^#c1:/c1:/" /etc/inittab

# Add overlay to boot critical services
rc-update add overlay boot

cat > /etc/runlevels/boot/.critical << EOF
checkroot overlay hostname modules checkfs localmount
EOF

# Active serial console ttyS0 for soekris
sed -i "s/^#s0:.*$/s0:12345:respawn:\/sbin\/agetty -L -i 19200 ttyS0 vt100/" /etc/inittab

